package com.magina.mq.rabbit.exchange;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 文件名称：Sender_02</br>
 * 初始作者： Magina.Hu</br>
 * 创建日期： 2019/5/10</br>
 * 功能说明： 模拟rabbitMq通过exchange发送mq <br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者 日期 修改内容<br/>
 * ================================================<br/>
 * Copyright (c) 2019-2020 .All rights reserved.<br/>
 */
@Component
public class RabExchangeSend {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    @Value("${rab.cass.config.exchange}")
    private String directExchange;

    @Value("${rab.mq.config.exchange}")
    private String topicExchange;

    @Value("${rab.cass.config.quene.info.routing.key}")
    private String infoKey;

    @Value("${rab.cass.config.quene.error.routing.key}")
    private String errorKey;


    /**
     * 方法描述:  模拟点对点发送 direct类型的Exchange路由规则也很简单，它会把消息路由到那些binding key与routing key完全匹配的Queue中。 </br>
     * 初始作者: Magina.Hu<br/>
     * 创建日期: 2019/5/10 <br/>
     * 开始版本: 1.0.0<br/>
     * =================================================<br/>
     * 修改记录：<br/>
     * 修改作者 日期 修改内容<br/>
     * ================================================<br/>
     * @param  入参对象
     * @return  返回数据
     * @throws Exception 异常
     */
    public void sendWithExchange(Integer type,Integer i ){
        String msg = "rabbitMq send direct exchange queue msg: type :" ;
        if(1 == type)
        this.rabbitTemplate.convertAndSend(this.directExchange,this.infoKey,String.format("%s %s.count: %s",msg,infoKey,i));
        if(2 == type)
        this.rabbitTemplate.convertAndSend(this.directExchange,this.errorKey,String.format("%s %s.count: %s",msg,errorKey,i));
    }

    private static final String PRODUCT_TOPIC_QEUE_NAME_01 = "product.log.info";
    private static final String PRODUCT_TOPIC_QEUE_NAME_02 = "product.log.debug";
    private static final String PRODUCT_TOPIC_QEUE_NAME_03 = "product.log.error";
    private static final String PRODUCT_TOPIC_QEUE_NAME_04 = "product.log.warn";

    //模拟来自产品的四种日志类型
    public void sendProductLogWithExchange(Integer i ) {
        String msg = "rabbitMq send topic exchange queue msg." ;
        this.rabbitTemplate.convertAndSend(this.topicExchange, PRODUCT_TOPIC_QEUE_NAME_01, String.format("%s queueName: %s .count :%s",msg,PRODUCT_TOPIC_QEUE_NAME_01,i));
        this.rabbitTemplate.convertAndSend(this.topicExchange, PRODUCT_TOPIC_QEUE_NAME_02, String.format("%s queueName: %s .count :%s",msg,PRODUCT_TOPIC_QEUE_NAME_02,i));
        this.rabbitTemplate.convertAndSend(this.topicExchange, PRODUCT_TOPIC_QEUE_NAME_03, String.format("%s queueName: %s .count :%s",msg,PRODUCT_TOPIC_QEUE_NAME_03,i));
        this.rabbitTemplate.convertAndSend(this.topicExchange, PRODUCT_TOPIC_QEUE_NAME_04, String.format("%s queueName: %s .count :%s",msg,PRODUCT_TOPIC_QEUE_NAME_04,i));
    }

    private static final String ORDER_TOPIC_QEUE_NAME_01 = "order.log.info";
    private static final String ORDER_TOPIC_QEUE_NAME_02 = "order.log.debug";
    private static final String ORDER_TOPIC_QEUE_NAME_03 = "order.log.error";
    private static final String ORDER_TOPIC_QEUE_NAME_04 = "order.log.warn";

    //模拟来自订单的四种日志类型
    public void sendOrderLogWithExchange(Integer i ) {
        String msg = "rabbitMq send topic exchange queue msg." ;
        this.rabbitTemplate.convertAndSend(this.topicExchange, ORDER_TOPIC_QEUE_NAME_01, String.format("%s queueName: %s .count :%s",msg,ORDER_TOPIC_QEUE_NAME_01,i));
        this.rabbitTemplate.convertAndSend(this.topicExchange, ORDER_TOPIC_QEUE_NAME_02, String.format("%s queueName: %s .count :%s",msg,ORDER_TOPIC_QEUE_NAME_02,i));
        this.rabbitTemplate.convertAndSend(this.topicExchange, ORDER_TOPIC_QEUE_NAME_03, String.format("%s queueName: %s .count :%s",msg,ORDER_TOPIC_QEUE_NAME_03,i));
        this.rabbitTemplate.convertAndSend(this.topicExchange, ORDER_TOPIC_QEUE_NAME_04, String.format("%s queueName: %s .count :%s",msg,ORDER_TOPIC_QEUE_NAME_04,i));
    }

}
